package com.base.controller;

import java.io.PrintWriter;
import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.service.FreeroutesService;
import com.base.service.LogsService;
import com.base.utils.IP;

@Controller
@RequestMapping("/admin")
public class content_tianjiafeilv {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private FreeroutesService freeroutesService;

	// 电话回拨页面
	@RequestMapping("/content_tianjiafeilv")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		if (request.getSession().getAttribute("adminUserId") == null) {
			modelAndView.setViewName("jsp/login");
			return modelAndView;
		}
		modelAndView.setViewName("jsp/content_tianjiafeilv");
		return modelAndView;
	}

	String SUCCESS = "success";

	@RequestMapping(value = "/insertFreeroute", method = RequestMethod.POST)
	public void insertFreeroute(HttpServletRequest request, HttpServletResponse response, String name,
			BigDecimal feelRoute, Integer ratelength) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		int resultCode = freeroutesService.insert(name, feelRoute, true, ratelength);
		if (resultCode == 1) {
			logsService.insert(CommContent.TYPE_CREATE, "成功创建费率：" + name, usernamesys, true, IP.getIpAddr(request));
			out.print(SUCCESS);
		} else {
			logsService.insert(CommContent.TYPE_CREATE, "创建费率：" + name, usernamesys + "时失败了", true,
					IP.getIpAddr(request));
			out.print("添加失败，请您重试");
		}
	}

}
